package xyz.erupt.flow.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.erupt.core.annotation.EruptRouter;
import xyz.erupt.core.constant.EruptRestPath;
import xyz.erupt.core.view.EruptApiModel;
import xyz.erupt.flow.bean.entity.*;
import xyz.erupt.flow.constant.FlowConstant;
import xyz.erupt.flow.data.dto.DTO_OaWorkFlowDefinition;
import xyz.erupt.flow.data.interfaces.Create;
import xyz.erupt.flow.data.interfaces.Update;
import xyz.erupt.flow.data.mapper.OaWorkFlowDefinitionMapper;
import xyz.erupt.flow.data.vo.VO_OaWorkFlowDefinition;
import xyz.erupt.flow.service.WorkFlowDefinitionService;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @description:
 * @author: PP
 * @date: 2024/10/20 20:29
 * @param: 
 * @return: 
**/
@Validated
@RestController
@RequestMapping(EruptRestPath.ERUPT_API + "/" + FlowConstant.SERVER_NAME)
public class WorkFlowDefinitionController {

    @Autowired
    private WorkFlowDefinitionService workFlowDefinitionService;



//    /**
//     * 启动流程，会启动当前流程并构建工作流实例
//     * @return
//     */
//    @PostMapping("/def/start/{DefId}")
//    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
//    public EruptApiModel startById(@PathVariable Long DefId, @RequestBody JSONObject formContent) {
//        eruptWorkFlowEngine.startWorkFlow(DefId,formContent.toJSONString());
//        return EruptApiModel.successApi();
//    }

//    /**
//     * 预览流程时间线
//     * @return
//     */
//    @PostMapping("/process/timeline/preview/{formDefId}")
//    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
//    public EruptApiModel preview(@PathVariable("formDefId") String formDefId, @RequestBody JSONObject formContent) {
//        List<OaProcessActivityHistory> activities = processDefinitionService.preview(formDefId, formContent);
//        return EruptApiModel.successApi(activities);
//    }
//
//    /**
//     * 查看流程实例的时间线
//     * @return
//     */
//    @PostMapping("/process/timeline/{instId}")
//    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
//    public EruptApiModel timeline(@PathVariable("instId") Long instId) {
//        List<OaProcessActivityHistory> activities = processDefinitionService.preview(instId);
//        return EruptApiModel.successApi(activities);
//    }

    /**
     * 创建新的流程定义
     * @param workFlowDefinition
     * @return
     */
    @PostMapping("/admin/def")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel create(@RequestBody @Validated(value = Create.class) DTO_OaWorkFlowDefinition workFlowDefinition){
        workFlowDefinitionService.create(workFlowDefinition);
        return EruptApiModel.successApi();
    }

    /**
     * 查询流程定义数据
     * @param processKey 流程定义键
     * @return 流程定义数据
     */
    @GetMapping("/admin/def/detail/{processKey}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel getByProcessKey(@PathVariable String processKey){
        OaWorkFlowDefinition workFlowDefinition = workFlowDefinitionService.getByProcessKey(processKey);
        VO_OaWorkFlowDefinition vo = OaWorkFlowDefinitionMapper.INSTANCE.toVo(workFlowDefinition);
        return EruptApiModel.successApi(vo);
    }

    /**
     * 删除流程
     * @param processKey 流程键
     * @return 操作结果
     */
    @DeleteMapping("/admin/def/{processKey}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel remove(@PathVariable String processKey){
        workFlowDefinitionService.removeByProcessKey(processKey);
        return EruptApiModel.successApi();
    }

    /**
     * 编辑流程详情
     * @param template 流程模板信息
     * @return 修改结果
     */
    @PutMapping("/admin/def/detail")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel updateDefDetail(@RequestBody @Validated(value = Update.class) DTO_OaWorkFlowDefinition template){
        workFlowDefinitionService.update(template);
        return EruptApiModel.successApi();
    }

    /**
     * 流程排序
     * @param defIds 定义流程ID
     * @return 排序结果
     */
    @PutMapping("/admin/def/sort")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel DefSort(@RequestBody @NotNull List<Long> defIds){
        workFlowDefinitionService.DefSort(defIds);
        return EruptApiModel.successApi();
    }

    @PutMapping("/admin/def/stop/{processKey}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel StopDef(@PathVariable String processKey){
        workFlowDefinitionService.StopDef(processKey);
        return EruptApiModel.successApi();
    }

    @PutMapping("/admin/def/start/{processKey}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel StartDef(@PathVariable String processKey){
        workFlowDefinitionService.StartDef(processKey);
        return EruptApiModel.successApi();
    }

}
